package com.ganmiao.web.portal.controller.file;

import com.ganmiao.annotation.NoAuthorization;
import com.ganmiao.resp.CommonResultRes;
import com.ganmiao.server.file.enums.GmAttachmentBizEnums;
import com.ganmiao.server.file.service.GmAttachmentService;
import com.ganmiao.server.file.vo.UploadFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Api(tags = "附件接口")
@RestController
@RequestMapping("/attachment")
@Slf4j
public class AttachmentController {

    @Autowired
    private GmAttachmentService gmAttachmentService;

    @NoAuthorization
    @ApiOperation("上传，返回地址")
    @PostMapping("/uploadFile")
    public CommonResultRes<UploadFileVo> uploadSiteInfoRepairFile(@ApiParam(value = "附件文件", required = true) MultipartFile multipartFile, @ApiParam("附件类型 default:默认 user:用户信息附件 course:课程附件 training:实习实训 teachingResource:教研资料") String bizType, HttpServletRequest request) {
        GmAttachmentBizEnums instance = GmAttachmentBizEnums.getInstance(bizType);
        if (Objects.isNull(instance)) {
            instance = GmAttachmentBizEnums.BIZ_DEFAULT;
        }
        return new CommonResultRes(gmAttachmentService.uploadFile(multipartFile, request, instance));
    }


//    @ApiOperation("下载附件")
//    @GetMapping("/downloadFile")
//    @Deprecated
//    public void downloadFile(
//            HttpServletResponse response, @RequestParam(value = "fileName") String fileName, @RequestParam(value = "fileUrl") String fileUrl) {
//        ServletOutputStream out = null;
//        try {
////            response.setCharacterEncoding("UTF-8");
////            response.setHeader("content-Type", "multipart/form-data");
////            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//            response.setContentType("multipart/form-data");
//            //与服务器建立连接
//            URL url = new URL(fileUrl);
//            URLConnection conn = url.openConnection();
//            InputStream inputStream = conn.getInputStream();
//            out = response.getOutputStream();
//            // 读取文件流
//            int len = 0;
//            byte[] buffer = new byte[1024 * 10];
//            while ((len = inputStream.read(buffer)) != -1) {
//                out.write(buffer, 0, len);
//            }
//            out.flush();
//            out.close();
//            inputStream.close();
//        } catch (Exception e) {
//            log.error("下载附件失败！fileUrl=" + fileUrl, e.getMessage(), e);
//            throw new DefaultBusinessException("附件不存在，下载失败！");
//        }
//    }

}
